package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 包名：com.itheima.health.controller
 *
 * @author zy
 * @date 2020 -09-04 16:01
 */

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 编辑权限
     * @param permission
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Permission permission){
        permissionService.update(permission);
        return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
        permissionService.add(permission);
        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Permission> pageResult = permissionService.findPage(queryPageBean);
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,pageResult);
    }

    /**
     * 通过id查询权限
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){
        Permission permission = permissionService.findById(id);
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
    }

    /**
     * 通过id删除权限
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        permissionService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    /**
     * 查询所有权限信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Permission> permissionList = permissionService.findAll();
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
    }
}
